package fei.yuzhi.com.loveflowers.fragment.mefragment.login.forgetPass;

import android.content.Context;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.UpdateListener;
import fei.yuzhi.com.loveflowers.fragment.mefragment.login.register.UserBean;

/**
 * Created by Administrator on 2016/10/7.
 */
public class ForgetPassModel implements ForgetPassContract.ModelForgetPass{

    public interface sendSituation{
            void setSituation(String msg);
    }
    public interface sendRegister{
        void setRegister(String msg);
    }

    @Override
    public void sendYanZhengMa(Context context, String tell, ForgetPassModel.sendSituation Situation) {
//            BmobSMS.requestSMSCode(context, tell, "验证码", new RequestSMSCodeListener() {
//
//                @Override
//                public void done(Integer integer, cn.bmob.sms.exception.BmobException e) {
//                    // TODO Auto-generated method stub
//                    if (e == null) {//验证码发送成功
//                        Log.i("bmob", "短信id：" + integer);//用于查询本次短信发送详情
//                       // Toast.makeText(context, "短信已发送", Toast.LENGTH_SHORT).show();
//                        Situation.setSituation("短信已发送");
//                    }else {
//                        Log.i("bmob", "发送失败");
//                        Situation.setSituation("短信发送失败");
//                    }
//                }
//            });

    }

    @Override
    public void userPass(Context context, final String name, final String newPass, final String tell, final ForgetPassModel.sendRegister register) {

        BmobQuery<UserBean> query = new BmobQuery<>();
        query.addWhereEqualTo("name",name);

        query.setLimit(50);
        query.findObjects(new FindListener<UserBean>() {
            @Override
            public void done(List<UserBean> list, BmobException e) {
                String id=list.get(0).getObjectId();

                UserBean p2 = new UserBean();
                p2.setPass(newPass);
                p2.update(id, new UpdateListener() {

                    @Override
                    public void done(BmobException e) {
                        if(e==null){
                            //toast("更新成功:"+p2.getUpdatedAt());
                            register.setRegister("修改成功");
                        }else{
                           // toast("更新失败：" + e.getMessage());
                            register.setRegister("修改失败");
                        }
                    }

                });

            }
        });

    }
}
